package com.uni.crawl.controller.crawl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uni.crawl.common.annotation.PreventDuplicateSubmit;
import com.uni.crawl.common.result.PageResult;
import com.uni.crawl.common.result.Result;
import com.uni.crawl.model.form.crawl.CrawlTemplateForm;
import com.uni.crawl.model.form.crawl.CrawlTemplateMockForm;
import com.uni.crawl.model.query.crawl.CrawlTemplatePageQuery;
import com.uni.crawl.model.vo.crawl.CrawlTemplateMockRunVO;
import com.uni.crawl.model.vo.crawl.CrawlTemplateVO;
import com.uni.crawl.service.CrawlTemplateCmd;
import com.uni.crawl.service.crawl.CrawlTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "爬虫模板管理接口")
@RestController
@RequestMapping("/api/v1/crawl/template")
@RequiredArgsConstructor
@Slf4j
public class CrawlTemplateController {

    private final CrawlTemplateService crawlTemplateService;
    private final CrawlTemplateCmd crawlTemplateCmd;

    @Operation(summary = "爬虫模板分页列表")
    @PostMapping("/page")
    public PageResult<CrawlTemplateVO> getPage(@RequestBody CrawlTemplatePageQuery pageQuery) {
        Page<CrawlTemplateVO> result = crawlTemplateService.getPage(pageQuery);
        return PageResult.success(result);
    }

    @Operation(summary = "爬虫模板表单数据")
    @GetMapping("/{id}/form")
    public Result<CrawlTemplateForm> getForm(
            @Parameter(description = "ID") @PathVariable Long id) {
        CrawlTemplateForm formData = crawlTemplateService.getForm(id);
        return Result.success(formData);
    }

    @Operation(summary = "新增爬虫模板", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("add")
    @PreAuthorize("@ss.hasPerm('crawl:template:add')")
    @PreventDuplicateSubmit
    public Result<Boolean> add(@RequestBody CrawlTemplateForm form) {
        return Result.judge(crawlTemplateService.add(form));
    }

    @Operation(summary = "修改爬虫模板")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('crawl:template:edit')")
    public Result<Boolean> update(@PathVariable Long id, @RequestBody CrawlTemplateForm form) {
        return Result.judge(crawlTemplateService.update(id, form));
    }

    @Operation(summary = "删除爬虫模板", description = "软删除")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('crawl:template:delete')")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.judge(crawlTemplateService.delete(id));
    }

    @Operation(summary = "开启|关闭爬虫模板")
    @PutMapping("/{id}/switchStatus")
    @PreAuthorize("@ss.hasPerm('crawl:template:switch:status')")
    public Result<Boolean> switchStatus(@PathVariable Long id) {
        return Result.judge(crawlTemplateService.switchStatus(id));
    }

    @Operation(summary = "执行一次")
    @PutMapping("/{id}/run")
    @PreAuthorize("@ss.hasPerm('crawl:template:run')")
    public Result<Boolean> run(@PathVariable Long id) {
        return Result.judge(crawlTemplateCmd.run(id));
    }

    @Operation(summary = "模拟执行")
    @PostMapping("/mockRun")
    @PreAuthorize("@ss.hasPerm('crawl:template:mock:run')")
    public Result<CrawlTemplateMockRunVO> mockRun(@RequestBody CrawlTemplateMockForm form) {
        try {
            return Result.success(crawlTemplateService.mockRun(form));
        } catch (Exception e) {
            log.info("模拟出现异常", e);
            CrawlTemplateMockRunVO vo = new CrawlTemplateMockRunVO();
            vo.setExceptionMockResult(e.getMessage());
            return Result.success(vo);
        }
    }
}
